package com.yidianyanglao.core.util;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.OSSObject;
import com.aliyun.oss.model.OSSObjectSummary;
import com.aliyun.oss.model.ObjectListing;
import com.aliyun.oss.model.ObjectMetadata;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

/**
 * 用于操作oss资源的工具类,该工具类需要读取aliyun.properties配置文件来加载连接oss系统的参数
 * 如果连接参数有变，请直接修改该配置文件即可
 *
 * @author shixiaodong
 */
public class OSSUtil {

	private static final Logger LOG = LoggerFactory.getLogger(OSSUtil.class);

	private static String endpoint;
	private static String accessKeyId;
	private static String accessKeySecret;
	private static String bucketName;
	private static OSSClient ossClient;

	/**
	 * oss中文件地址的前缀
	 */
	public static String OSS_URLPREFIX;
	public static String FILEPATH;

	static {
		System.out.println("加载配置文件");
		Properties prop = new Properties();
		try {

			prop.load(OSSUtil.class.getClassLoader().getResourceAsStream("aly_oss.properties"));
			endpoint = prop.getProperty("endpoint");
			accessKeyId = prop.getProperty("accessKeyId");
			accessKeySecret = prop.getProperty("accessKeySecret");
			bucketName = prop.getProperty("bucketName");
			OSS_URLPREFIX = prop.getProperty("urlPrefix");
			FILEPATH = prop.getProperty("filePath");
			ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
			System.out.println("初始化完成");
		} catch (IOException e) {
			e.printStackTrace();
			LOG.error("load error ==> aly_oss.properties", e.getMessage(), e);
		}
	}

	/**
	 * 上传资源到oss指定路径下
	 * @param input 输入流对象
	 * @param key 该资源key
	 * @param size 该文件的大小
	 */
	public static void uploadFile(InputStream input,String key,long size) {
		// 创建上传Object的Metadata
		ObjectMetadata meta = new ObjectMetadata();
		// 必须设置ContentLength
		meta.setContentLength(size);
		// 上传Object.
		ossClient.putObject(bucketName, key, input, meta);
	}

	/**
	 * 通过资源key删除该资源
	 * @param key
	 */
	public static void deleteFile(String key) {
		ossClient.deleteObject(bucketName, key);
	}

	/**
	 * 根据oss 路径前缀获得该路径下的所有资源的key的List集合
	 * @param prefix
	 * @return
	 */
	public static List<String> listFile(String prefix){
		List<String> list = new ArrayList<String>();
		ObjectListing olist = ossClient.listObjects(bucketName, prefix);
		// 遍历所有Object
		for (OSSObjectSummary objectSummary : olist.getObjectSummaries()) {
			list.add(objectSummary.getKey());
		}
		return list;
	}

	/**
	 * 从oss中下载文件
	 * @param filePath oss key
	 * @param downloadPath 下载文件存放路径
	 * @throws IOException
	 */
	public static void download(String filePath,String downloadPath) throws IOException{
		// 获取Object，返回结果为OSSObject对象
		OSSObject object = ossClient.getObject(bucketName, filePath);
		// 获取Object的输入流
		InputStream input = null;
		FileOutputStream out = null;
		try{
			input = object.getObjectContent();
			File file = new File(downloadPath);
			if(file.exists()){
				file.delete();
			}
			out = new FileOutputStream(file);
			int i = 1;
			while(i >= 0){
				i = input.read();
				out.write(i);
			}
		}catch(IOException e){
			throw new IOException();
		}finally{
			if(out != null) {out.flush(); out.close();}
			if(input != null) input.close();
		}
	}

	/**
	 * Description: 判断OSS服务文件上传时文件的contentType
	 * @param FilenameExtension 文件后缀
	 * @return String
	 */
	public static String contentType(String FilenameExtension){
		if(FilenameExtension.equals("BMP") || FilenameExtension.equals("bmp"))
			return "image/bmp";
		if(FilenameExtension.equals("GIF") || FilenameExtension.equals("gif"))
			return "image/gif";
		if(FilenameExtension.equals("JPEG") || FilenameExtension.equals("jpeg") ||
				FilenameExtension.equals("JPG") || FilenameExtension.equals("jpg") ||
				FilenameExtension.equals("PNG") || FilenameExtension.equals("png"))
			return "image/jpeg";
		if(FilenameExtension.equals("HTML") || FilenameExtension.equals("html"))
			return "text/html";
		if(FilenameExtension.equals("TXT") || FilenameExtension.equals("txt"))
			return "text/plain";
		if(FilenameExtension.equals("VSD") || FilenameExtension.equals("vsd"))
			return "application/vnd.visio";
		if(FilenameExtension.equals("PPTX") || FilenameExtension.equals("pptx") ||
				FilenameExtension.equals("PPT") || FilenameExtension.equals("ppt"))
			return "application/vnd.ms-powerpoint";
		if(FilenameExtension.equals("DOCX") || FilenameExtension.equals("docx") ||
				FilenameExtension.equals("DOC") || FilenameExtension.equals("doc"))
			return "application/msword";
		if(FilenameExtension.equals("XML") || FilenameExtension.equals("xml"))
			return "text/xml";
		return "text/html";
	}

}